<template>
  <div class="app-container">
    <el-table v-loading="loading" :data="retirementAgreementList">
      <el-table-column label="首页头部logo" align="center" prop="homeHeadLogo" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.homeHeadLogo" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="是否置灰" align="center" key="isGrayOut">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.isGrayOut" active-value="1" inactive-value="0" @change="handleIsGrayOutChange(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="关于我们" align="center" prop="aboutUs" width="200">
        <template slot-scope="scope">
          <div class="text-truncate" :title="scope.row.aboutUs" @click="showContentDialog('关于我们', scope.row.aboutUs)">
            {{ scope.row.aboutUs }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="用户协议" align="center" prop="userAgreement" width="200">
        <template slot-scope="scope">
          <div class="text-truncate" :title="scope.row.userAgreement" @click="showContentDialog('用户协议', scope.row.userAgreement)">
            {{ scope.row.userAgreement }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="隐私协议" align="center" prop="privacyPolicy" width="200">
        <template slot-scope="scope">
          <div class="text-truncate" :title="scope.row.privacyPolicy" @click="showContentDialog('隐私协议', scope.row.privacyPolicy)">
            {{ scope.row.privacyPolicy }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['retirement:retirementAgreement:edit']"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改界面与协议对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
<!--        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="首页头部logo" prop="homeHeadLogo">
              <image-upload v-model="form.homeHeadLogo" :limit="1"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否置灰" prop="isGrayOut">
              <el-select v-model="form.isGrayOut" placeholder="请选择是否置灰" style="width: 100%">
                <el-option label="是" value="1"></el-option>
                <el-option label="否" value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>-->
        <el-form-item label="首页头部logo" prop="homeHeadLogo">
          <image-upload v-model="form.homeHeadLogo" :limit="1"/>
        </el-form-item>
        <el-form-item label="是否置灰" prop="isGrayOut">
          <el-radio-group v-model="form.isGrayOut">
            <el-radio label="1">是</el-radio>
            <el-radio label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="关于我们" prop="aboutUs">
          <el-input
            v-model="form.aboutUs"
            type="textarea"
            :rows="6"
            placeholder="请输入关于我们"
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="用户协议" prop="userAgreement">
          <el-input
            v-model="form.userAgreement"
            type="textarea"
            :rows="6"
            placeholder="请输入用户协议"
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="隐私协议" prop="privacyPolicy">
          <el-input
            v-model="form.privacyPolicy"
            type="textarea"
            :rows="6"
            placeholder="请输入隐私协议"
            style="width: 100%"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 内容展示弹框 -->
    <el-dialog :title="contentDialog.title" :visible.sync="contentDialog.visible" width="60%" append-to-body>
      <div class="content-display">
        <pre>{{ contentDialog.content }}</pre>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="contentDialog.visible = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRetirementAgreement,
  getRetirementAgreement,
  delRetirementAgreement,
  addRetirementAgreement,
  updateRetirementAgreement,
  changeIsGrayOut
} from "@/api/retirement/retirementAgreement"

export default {
  name: "RetirementAgreement",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 界面与协议表格数据
      retirementAgreementList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 内容展示弹框
      contentDialog: {
        visible: false,
        title: '',
        content: ''
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        homeHeadLogo: null,
        isGrayOut: null,
        aboutUs: null,
        userAgreement: null,
        privacyPolicy: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        isGrayOut: [
          { required: true, message: "是否置灰不能为空", trigger: "change" }
        ],
        aboutUs: [
          { required: true, message: "关于我们不能为空", trigger: "blur" }
        ],
        userAgreement: [
          { required: true, message: "用户协议不能为空", trigger: "blur" }
        ],
        privacyPolicy: [
          { required: true, message: "隐私协议不能为空", trigger: "blur" }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询界面与协议列表 */
    getList() {
      this.loading = true
      listRetirementAgreement(this.queryParams).then(response => {
        this.retirementAgreementList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        homeHeadLogo: null,
        isGrayOut: null,
        aboutUs: null,
        userAgreement: null,
        privacyPolicy: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      }
      this.resetForm("form")
    },


    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getRetirementAgreement(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改界面与协议"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRetirementAgreement(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addRetirementAgreement(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    // 用户状态修改
    handleIsGrayOutChange(row) {
      console.info(row)
      /*row.isGrayOut = row.isGrayOut === "0" ? "1" : "0"*/
      let text = row.isGrayOut === "0" ? "取消置灰" : "置灰"
      this.$modal.confirm('确认要"' + text + '"全局吗？').then(function() {
        return changeIsGrayOut(row.id, row.isGrayOut)
      }).then(() => {
        this.$modal.msgSuccess(text + "成功")
      }).catch(function() {
        row.isGrayOut = row.isGrayOut === "0" ? "1" : "0"
      })
    },
    // 显示内容弹框
    showContentDialog(title, content) {
      this.contentDialog.title = title
      this.contentDialog.content = content
      this.contentDialog.visible = true
    },
  }
}
</script>

<style scoped>
.text-truncate {
  max-width: 180px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  cursor: pointer;
  color: #409EFF;
}

.text-truncate:hover {
  color: #66b1ff;
}

.content-display {
  max-height: 500px;
  overflow-y: auto;
  padding: 10px;
  background-color: #f5f5f5;
  border-radius: 4px;
}

.content-display pre {
  white-space: pre-wrap;
  word-wrap: break-word;
  margin: 0;
  font-family: inherit;
  line-height: 1.5;
}
</style>
